<template>
  <div>
      <el-dialog
        title="选择公式"
        :visible.sync="dialogVisible"
        append-to-body
        width="50%">
        <el-row>
          <el-col :span="24">
              <el-form :model="form"  class="demo-form-inline" label-width="100px">
                <el-col :span="24">
                  <el-form-item label="公式">
                    <el-select v-model="form.gsId" placeholder="请选择" clearable
                                 @change="elGsChange()" class="xiaLa" style="width:100%;">
                        <el-option :label="'平均分配'" :value="-1">
                        </el-option>
                        <el-option v-for="(item1,index) in gsData " :key="index" :label="item1.gsMc" :value="item1.id">
                        </el-option>
                      </el-select>
                </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-col :span="20">
                    <el-form-item label="算法展示" prop="gsZs">
                      <el-input  readonly v-model="form.gsZs" style="width:100%;" placeholder="算法展示">
                      </el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="4" v-if="form.gsId!=-1">
                    <el-button type="primary" plain style="margin-left:10px"
                         @click="configHandle(form.gsId)">修改
                  </el-button>
                  </el-col>
                </el-col>
               <el-col :span="12" v-if="text=='geren'">
                    <el-form-item>
                      <el-checkbox v-model="form.jkjbj">含加扣奖</el-checkbox>
                    </el-form-item>
                </el-col>
              </el-form>
          </el-col>
        </el-row>
        <span slot="footer" class="dialog-footer">
              <el-button type="primary" @click="submint()">确 定</el-button>
              <el-button @click="close()">取 消</el-button>
          </span>
     </el-dialog>
     
      <config-form ref="configForm" @refreshDataList="refreshDataList"></config-form>
    </div>

</template>

<script>
import {getJjDeptJjgtoPrant} from '@/api/admin/sysformula'
import { getFzList,saveGs} from '@/api/jiangjin/jiangjinryfzmxb'
import configForm from '@/views/admin/sysformula/sysformula-form-config'
import { fetchList} from '@/api/admin/sysformula'

export default {
    data(){
        return{
            dialogVisible:false,
            dialogVisible1:false,
            configVisible:false,
            form:{},
            gsData:[],
            deptId:'',
            jiList:[],
            row:{},
            getSpanArr:[],
            text:''
        }
    },
    components: {
      configForm
    },
    methods:{
        init(row,text){
            this.dialogVisible=true;
            this.row=row;
            this.text=text
            if(this.text=='geren'){
            if(this.row.jkjbj==1){
              this.$set(this.form,'jkjbj',true)
            }else{
              this.$set(this.form,'jkjbj',false)
            }
          }
            this.$set(this.form,'gsId',row.gsId)
            this.getGsListOption(text)
            //this.getRenJi(row.id)
        },
        close(){
          this.dialogVisible=false
          this.form.gsId=''
          this.form.gsZs=''
        },
        refreshDataList(){
          this.getGsListOption(this.text)
        },
        //配置
        configHandle(id) {
          this.configVisible=true;
          this.$refs.configForm.init(id)
        },
        getDataList1(page) {
          if (page) {
            this.pageIndex = page
          }
          this.getDataList(this.deptId, this.fl)
        },
        // 获取数据列表
        getDataList(deptId, type) {
          this.dataListLoading = true
          this.$set(this.dataForm, "jjDeptId", deptId);
          this.dataForm.gsType = type;
          fetchList(Object.assign({
            current: this.pageIndex,
            size: this.pageSize
          }, this.dataForm)).then(response => {
            this.dataList = response.data.data.records
            this.totalPage = response.data.data.total
          })
          this.dataListLoading = false
        },
        //提交
        submint(){
          if(!this.form.gsId){
            this.$message.closeAll()
            this.$message.warning('请选择公式!')
            return false;
          }
          let names;
          //gsMc
          if(this.form.gsId!=-1){
           names= this.gsData.filter(item=>{
              return item.id==this.form.gsId
            })

            this.$set(this.row,'gsName',names[0].gsMc)
          }else{
            this.$set(this.row,'gsName','平均分配')
          }

          if(this.text=='geren'){
            if(this.form.jkjbj){
              this.$set(this.row,'jkjbj',1)
            }else{
              this.$set(this.row,'jkjbj',0)
            }
          }

          this.$set(this.row,'gsId',this.form.gsId)

          saveGs(this.row).then(res=>{
            if(res.data.code==0){
              this.$message.closeAll()
              this.$message.success('选择成功!')
              this.close()
              this.$emit('chaifenChild')
            }
          })
        },
        closeTwo(){
          this.dialogVisible1 = false;
        },
        elGsChange(){
         // this.dialogVisible1 = true;
         if(this.form.gsId=='-1'){
                this.$set(this.form,'gsZs','')
          }else{
            this.gsData.forEach(item=>{
            if(this.form.gsId==item.id){
              this.$set(this.form,'gsZs',item.gsZs)
              
            }
          })
          }
          
         
        },
        getRenJi(fzid){
          getFzList(fzid).then(res=>{
            this.renList=res.data.data;
            this.renList=this.renList.map(item=>{
                item.jjJe=0;
                return item
            })
           // this.pingJun()
          })
          jiGougetFzList(fzid).then(res=>{
            this.jiList=res.data.data.map(item=>{
                item.jjJe=0;
                item.deptName=item.jgDeptName
                item.deptCode=item.jgDeptCode
                return item
            })
            this.pingJun()
          })
      },
         //取得基本数据
      getGsListOption(text) {
        if(text=='geren'){
          var params={
              id: this.deptId,
              gsType: '奖金算法',
              gsFl: '个人'
          }
        }
        if(text=='jigou'){
          var params={
              id: this.deptId,
              getJJxNamesType: '奖金算法',
              gsFl: '部门'
          }
        }
        //取得公式数组
        //取得公式数组
        getJjDeptJjgtoPrant(params).then(res => {
          this.gsData = res.data.data;
          this.gsData.forEach(item=>{
            if(this.form.gsId==item.id){
              this.$set(this.form,'gsZs',item.gsZs)
            }
          })
        })
      },
    }
}
</script>

<style>

</style>
